/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts;

import com.dairy.database.DatabaseInterface;
import com.dairy.helpers.DatabaseHelper;
import com.dairy.structures.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author tecton
 */
public class LoginAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String userType;

        HttpSession session = request.getSession();
        Object object = session.getAttribute("userType");
        if (object != null) {
            userType = object.toString();
            return mapping.findForward(userType);
        }

        LoginForm formBean = (LoginForm)form;
        String username = formBean.getUsername();
        String password = formBean.getPassword();
        userType = formBean.getUserType();
        DatabaseInterface db = null;

        if (username.equals("") || password.equals("")) {
            return mapping.findForward(FAILURE);
        }
        
        User u = new User ();
        Milkman m = new Milkman ();
        Admin a = new Admin ();
        if ( userType.equals( "user" ) )
        {
            db = DatabaseHelper.getUserDatabase(servlet);
            u = db.getUser (username);
        }
        else if ( userType.equals( "milkman" ) )
        {
            db = DatabaseHelper.getMilkmanDatabase(servlet);
            m = db.getMilkman (Integer.parseInt(username));
        }
        else if ( userType.equals( "admin" ) )
        {
            db = DatabaseHelper.getAdminDatabase(servlet);
            a = db.getAdmin(username);
        }

        if ( userType.equals( "user" ) ){
            username = u.username;
            password = u.password;
        }
        else if ( userType.equals( "milkman" ) ){
            username = Integer.toString(m.job_number);
            password = m.password;
        }
        else if ( userType.equals( "admin" ) ){
            username = a.username;
            password = a.password;
        }
        
        if ( !formBean.getUsername().equals ( username ) || !formBean.getPassword().equals ( password ) )
            return mapping.findForward(FAILURE);

        session.setAttribute( "username",  username );
        session.setAttribute( "userType", userType );
        return mapping.findForward(userType);
    }
}
